// 定义订单状态的枚举类
enum OrderState {
    UNPAID, // 未支付
    PAID,   // 已支付
    SHIPPED, // 已发货
    COMPLETED // 已完成
}

// 定义一个状态机接口
interface StateMachine {
    void transition(String event);
    void printCurrentState();
}

// 定义一个订单类，它是状态机的一个实现
class OrderStateMachine implements StateMachine {
    private OrderState currentState;

    // 构造方法，初始状态为未支付
    public OrderStateMachine() {
        currentState = OrderState.UNPAID;
    }

    // 状态转换方法，根据事件触发状态变化
    @Override
    public void transition(String event) {
        switch (currentState) {
            case UNPAID:
                if ("pay".equals(event)) {
                    currentState = OrderState.PAID;
                    System.out.println("Order is paid.");
                } else {
                    System.out.println("Invalid event: " + event + " for current state: " + currentState);
                }
                break;
            case PAID:
                if ("ship".equals(event)) {
                    currentState = OrderState.SHIPPED;
                    System.out.println("Order is shipped.");
                } else if ("cancel".equals(event)) {
                    currentState = OrderState.UNPAID;
                    System.out.println("Order is canceled.");
                } else {
                    System.out.println("Invalid event: " + event + " for current state: " + currentState);
                }
                break;
            case SHIPPED:
                if ("complete".equals(event)) {
                    currentState = OrderState.COMPLETED;
                    System.out.println("Order is completed.");
                } else if ("cancel".equals(event)) {
                    currentState = OrderState.PAID;
                    System.out.println("Order is returned to PAID.");
                } else {
                    System.out.println("Invalid event: " + event + " for current state: " + currentState);
                }
                break;
            case COMPLETED:
                System.out.println("Order is already completed, no further transitions.");
                break;
            default:
                System.out.println("Unknown state: " + currentState);
        }
    }

    // 打印当前状态
    @Override
    public void printCurrentState() {
        System.out.println("Current state: " + currentState);
    }
}

// 主程序类
public class StateMachineExample {
    public static void main(String[] args) {
        // 创建一个订单状态机对象
        OrderStateMachine order = new OrderStateMachine();

        // 打印初始状态
        order.printCurrentState(); // 输出：Current state: UNPAID

        // 进行状态转换
        order.transition("pay");  // 输出：Order is paid.
        order.printCurrentState(); // 输出：Current state: PAID

        order.transition("ship"); // 输出：Order is shipped.
        order.printCurrentState(); // 输出：Current state: SHIPPED

        order.transition("complete"); // 输出：Order is completed.
        order.printCurrentState(); // 输出：Current state: COMPLETED

        order.transition("pay");  // 输出：Order is already completed, no further transitions.
    }
}
